let dic_inspur=function(){
    const init = function (dom,options){
        var dicDom = $(dom);
        var dic = options;
        var title = dic.title==null?"请选择":dic.title;;
        var url = dic.url;
        var code = dic.code==null?"code":dic.code;
        var content = dic.content==null?"content":dic.content;
        var value = dic.value;
        $.ajax({
            url: url,
            contentType: "application/json",
            dataType: "json",
            success: function (response) {
                initDic(title,dicDom,response,code,content,value);
                // if (response.status === 200) {
                // } else {
                //     alert(response.message ? response.message : ("请求接口发生错误：" + response.status));
                // }
            },
            error: function (xhr, textstatus, thrown) {
                console.log(xhr.status + ":" + xhr.statusText);
            }
        });
    }
    const initDic =  function (title,jqueryDom,data,code,content,value){
        var form = layui.form;
        if(jqueryDom.is('select')){
            jqueryDom.html('');
            jqueryDom.append('<option  value="">'+title+'</option>');
            $.each(data,function(i){
                if (value&&value==data[i][code]){
                    jqueryDom.append('<option selected value="'+data[i][code]+'">'+data[i][content]+'</option>');
                }else{
                    jqueryDom.append('<option value="'+data[i][code]+'">'+data[i][content]+'</option>');
                }
            });
        }else if(jqueryDom.is('input')){
            var inputHtml='';
            var name = jqueryDom.attr('name');

            var dic = eval('(' +jqueryDom.attr('dic')  + ')');
            var type = dic.type==null?"radio":dic.type;;


            if (type=='checkbox'){

                $.each(data,function(i){
                    if (value&&$.inArray(data[i][code], value.split(','))>-1){
                        inputHtml+='<input type="checkbox" name="'+name+'" checked lay-skin="primary"  title="'+data[i][content]+'" value="'+data[i][code]+'">';
                    }else{
                        inputHtml+='<input type="checkbox" name="'+name+'" lay-skin="primary"  title="'+data[i][content]+'" value="'+data[i][code]+'">';
                    }
                });
            }else if(type=='radio'){
                $.each(data,function(i){
                    if (value&&value==data[i][code]){
                        inputHtml+='<input type="radio" name="'+name+'" checked lay-skin="primary"  title="'+data[i][content]+'" value="'+data[i][code]+'">';
                    }else{
                        inputHtml+='<input type="radio" name="'+name+'" lay-skin="primary"  title="'+data[i][content]+'" value="'+data[i][code]+'">';
                    }
                });
            }
            jqueryDom.replaceWith(inputHtml);
        }else if(jqueryDom.is('label')){
            $.each(data,function(i){
                if (value&&value==data[i][code]) {
                    jqueryDom.html(data[i][content] );
                }
            });
        }
        form.render();
    }
    const render = function (){
        $('[dic]').each(function(){
            var dicDom = $(this);
            var dic = eval('(' +dicDom.attr('dic')  + ')');
            init(this,dic)
        });
    }
    render();
    return {
        init:init,
        render:render
    };
}();